<template>
  <div v-loading.fullscreen="loading">
    <el-row :gutter="10">
      <el-col
        :span="24"
        style="text-align:left"
      >
        <el-radio-group v-model="dialogDatas.filters.tpxGkRecord.type">
          <el-radio
            :value="0"
            border
          >刮刀</el-radio>
          <el-radio
            :value="1"
            border
          >钢网</el-radio>
        </el-radio-group>
      </el-col>

      <el-col
        :span="24"
        style="text-align:left"
        class="marginTop10"
      >
        <el-input
          v-model="dialogDatas.filters.tpxGkRecord.id"
          size="large"
          @change="dialogDatas.getSbId"
          clearable
        >
          <template #prepend>
            <span>设备ID</span>
          </template>
        </el-input>

        <el-input
          v-model="dialogDatas.filters.tpxGkRecord.bjNameOld"
          size="large"
          clearable
        >
          <template #prepend>
            <span>旧部件ID</span>
          </template>
        </el-input>

        <el-input
          v-model="dialogDatas.filters.tpxGkRecord.bjName"
          size="large"
          clearable
        >
          <template #prepend>
            <span>新部件ID</span>
          </template>
        </el-input>

        <el-input
          v-model="dialogDatas.filters.tpxGkRecord.limitNum"
          size="large"
          clearable
        >
          <template #prepend>
            <span>最大使用次数</span>
          </template>
        </el-input>
      </el-col>

    </el-row>

    <div
      class="marginTop10"
      style="margin-bottom: 20px;"
    >
      <el-button
        type="warning"
        size="large"
        @click="dialogDatas.confirmBtnClick(1)"
      >
        停机下线
      </el-button>

      <el-button
        type="success"
        size="large"
        @click="dialogDatas.confirmBtnClick(0)"
      >
        提交
      </el-button>
    </div>

  </div>
</template>
<script>
import { computed, reactive, ref } from 'vue';
import { onMounted, watch } from '@vue/runtime-core';
import { dayjs } from 'element-plus';

import myAxios from '@/myAxios/myAxios';
import myAxiosTPX from '@/myAxios/myAxiosTPX';
import { showSuccess, showWarning } from '@/hooks/useMyMessageBox';
import myAxios28 from '@/myAxios/myAxios28';
import { baseUrl } from '@/myAxios/api.config';
import store from '@/store';
import myAxios193 from '@/myAxios/myAxios193';

export default {
  name: 'DjRecordView',
  setup () {
    let loading = ref(false);

    let dialogDatas = reactive({
      filters: {
        tpxGkRecord: {
          id: '', // 设备ID
          sbName: '镭雕机', // 设备名称
          bjNameOld: '', // 旧部件ID
          bjName: '', // 新部件ID
          type: 1, // 0:刮刀 1:钢网
          limitNum: 100000, // 最大使用次数
          nowValue: 0, // 现在的使用次数
          limitTime: '', // 最大使用时间
          data1: '', // 激活时间
          data2: '', // 报废时间
          data3: '1', // VARCHAR2(1)
        },
        applyType: 1,
        applyno: '',
      },

      dialogFormVisible: false,
      currentData: {},
      isNew: false,
      confirmBtnClick: async function (confirmType) {
        if (!dialogDatas.filters.tpxGkRecord.id) {
          showWarning('请输入设备ID');
          return;
        }
        // 停机下线
        if (confirmType == 1) {
          if (!dialogDatas.filters.tpxGkRecord.bjNameOld) {
            showWarning('请输入旧部件ID');
            return;
          }

          let existOldDatas = await myAxios.get('陈帅廷查询贴片线设备管控信息', dialogDatas.filters.tpxGkRecord.bjNameOld);
          //旧部件已经存在
          if (existOldDatas.length > 0) {
            // 旧部件状态改为空闲
            let updateParam = '';
            let updateResult = '';
            let bjName = existOldDatas[0]['BJ_NAME'];
            let type = dialogDatas.filters.tpxGkRecord.type;
            updateParam = `${bjName}🈹0`;
            updateResult = await myAxios.post('陈帅廷修改贴片线设备状态', updateParam);
            // 钢网要修改清除倒计时
            if (type == 1) {
              let dataContent = {
                "timerType": 'gw',
                "qrcode": bjName,
                "state": 0,
                //0:开始,1:结束
                "type": 1,
              };
              await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
            }

            if (updateResult.includes('成功')) {
              loading.value = false;
              showSuccess('提交成功!');
              dialogDatas.filters.tpxGkRecord.id = '';
              dialogDatas.filters.tpxGkRecord.bjName = '';
              dialogDatas.filters.tpxGkRecord.bjNameOld = '';
              dialogDatas.filters.tpxGkRecord.limitNum = 100000;
            }
            else {
              loading.value = false;
              showWarning('提交失败!' + updateResult);
            }
          }
          else {
            loading.value = false;
            showWarning('旧部件不存在!');
          }
          return;
        }

        if (!dialogDatas.filters.tpxGkRecord.bjName) {
          showWarning('请输入部件ID');
          return;
        }
        loading.value = true;


        let existDatas = await myAxios.get('陈帅廷查询贴片线设备管控信息', dialogDatas.filters.tpxGkRecord.bjName);
        //新部件已经存在
        if (existDatas.length > 0) {
          // 新部件状态改为使用中
          let updateParam = '';
          let updateResult = '';
          let bjName = existDatas[0]['BJ_NAME'];
          let type = dialogDatas.filters.tpxGkRecord.type;
          if (type == 0) {
            let nowStr = dayjs().format('YYYY/MM/DD HH:mm:ss');
            let time4hour = dayjs().add(4, 'hours').format('YYYY/MM/DD HH:mm:ss');
            // let time4hour = dayjs().add(20, 'seconds').format('YYYY/MM/DD HH:mm:ss');
            updateParam = `${bjName}🈹1🈹${nowStr}🈹${time4hour}`;
            updateResult = await myAxios.post('陈帅廷更新贴片线设备状态和时间', updateParam);
            // let dataContent = {
            //   "timerType": 'gw',
            //   "qrcode": bjName,
            //   "state": 0,
            //   //0:开始,1:结束
            //   "type": 0,
            // };
            // await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
          }
          // 钢网要修改激活时间和清洗时间(4小时)
          else if (type == 1) {
            let nowStr = dayjs().format('YYYY/MM/DD HH:mm:ss');
            let time4hour = dayjs().add(4, 'hours').format('YYYY/MM/DD HH:mm:ss');
            // let time4hour = dayjs().add(20, 'seconds').format('YYYY/MM/DD HH:mm:ss');
            updateParam = `${bjName}🈹1🈹${nowStr}🈹${time4hour}`;
            updateResult = await myAxios.post('陈帅廷更新贴片线设备状态和时间', updateParam);

            let dataContent = {
              "timerType": 'gw',
              "qrcode": bjName,
              "state": 0,
              //0:开始,1:结束
              "type": 0,
            };
            await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
          }
          let updateParamOld = ``;
          let updateResultOld = '成功';

          if (dialogDatas.filters.tpxGkRecord.bjNameOld) {
            // 旧部件状态改为空闲
            updateParamOld = `${dialogDatas.filters.tpxGkRecord.bjNameOld}🈹0`;
            updateResultOld = await myAxios.post('陈帅廷修改贴片线设备状态', updateParamOld);
            if (type == 0) {
            }
            else if (type == 1) {
              let dataContent = {
                "timerType": 'gw',
                "qrcode": dialogDatas.filters.tpxGkRecord.bjNameOld,
                "state": 0,
                //0:开始,1:结束
                "type": 1,
              };
              await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
            }
          }
          if (updateResult.includes('成功') && updateResultOld.includes('成功')) {
            loading.value = false;
            ElMessage({
              showClose: true,
              type: 'success',
              message: '提交成功!',
            });
            dialogDatas.filters.tpxGkRecord.id = '';
            dialogDatas.filters.tpxGkRecord.bjName = '';
            dialogDatas.filters.tpxGkRecord.bjNameOld = '';
            dialogDatas.filters.tpxGkRecord.limitNum = 100000;
          }
        }
        else {
          // 不存在就添加
          let nowStr = dayjs().format('YYYY/MM/DD HH:mm:ss');
          let id = dialogDatas.filters.tpxGkRecord.id;
          let sbName = '';
          let limitTime = '';
          let type = dialogDatas.filters.tpxGkRecord.type;
          if (type == 0) {
            sbName = '刮刀';
          }
          else if (type == 1) {
            sbName = '钢网';
            limitTime = dayjs().add(4, 'hours').format('YYYY/MM/DD HH:mm:ss');
            // limitTime = dayjs().add(20, 'seconds').format('YYYY/MM/DD HH:mm:ss');
          }
          let selectParams = `${id}🈹${sbName}`
            + `🈹${dialogDatas.filters.tpxGkRecord.bjName}🈹${type}`
            + `🈹${dialogDatas.filters.tpxGkRecord.limitNum}🈹${dialogDatas.filters.tpxGkRecord.nowValue}`
            + `🈹${limitTime}🈹${nowStr}`
            + `🈹${dialogDatas.filters.tpxGkRecord.data2}🈹${dialogDatas.filters.tpxGkRecord.data3}`;
          let newDatas = await myAxios.post('陈帅廷添加或修改贴片线管控信息', selectParams, '9900');
          if (newDatas.includes('成功')) {
            // 钢网添加4小时倒计时
            if (type == 1) {
              let dataContent = {
                "timerType": 'gw',
                "qrcode": dialogDatas.filters.tpxGkRecord.bjName,
                "state": 0,
                //0:开始,1:结束
                "type": 0,
              };
              await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
            }

            if (dialogDatas.filters.tpxGkRecord.bjNameOld) {
              // 旧部件状态改为空闲,更新报废时间
              let bfParams = `${dialogDatas.filters.tpxGkRecord.bjNameOld}🈹${nowStr}🈹0`;
              let bfDatas = await myAxios.post('陈帅廷更新贴片线设备管控报废时间', bfParams);
              if (bfDatas.includes('成功')) {
                loading.value = false;
                // 钢网停止4小时倒计时
                if (type == 1) {
                  let dataContent = {
                    "timerType": 'gw',
                    "qrcode": dialogDatas.filters.tpxGkRecord.bjNameOld,
                    "state": 0,
                    //0:开始,1:结束
                    "type": 1,
                  };
                  await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
                }
                ElMessage({
                  showClose: true,
                  type: 'success',
                  message: '提交成功!',
                });
                dialogDatas.filters.tpxGkRecord.id = '';
                dialogDatas.filters.tpxGkRecord.bjName = '';
                dialogDatas.filters.tpxGkRecord.bjNameOld = '';
                dialogDatas.filters.tpxGkRecord.limitNum = 100000;
              }
              else {
                loading.value = false;
                showWarning('提交失败，请重试' + bfDatas);
              }
            }
            else {
              loading.value = false;
              ElMessage({
                showClose: true,
                type: 'success',
                message: '提交成功!',
              });
              dialogDatas.filters.tpxGkRecord.id = '';
              dialogDatas.filters.tpxGkRecord.bjName = '';
              dialogDatas.filters.tpxGkRecord.bjNameOld = '';
              dialogDatas.filters.tpxGkRecord.limitNum = 100000;
            }
          } else {
            loading.value = false;
            showWarning('添加失败' + newDatas);
          }

        }

      },
      getSbId (id) {
        let arr = id.split('EMCODE=');
        if (arr.length > 1) {
          dialogDatas.filters.tpxGkRecord.id = arr[1];
        }
        console.log('id :>> ', id);
      },
      cancelBtnClick: function () {

      }
    });

    return {
      loading,
      dialogDatas,
    };
  }
}
</script>
<style lang="css" scope>
.warning-row {
  background-color: var(--el-color-warning) !important;
}
.marginTop10 {
  margin-top: 10px;
}
</style>

